package com.sunwayworld.basemodule.business.operationbatch.service.impl;

import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.utils.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.operationbatch.dao.LimsPreTaskSupplementDao;
import com.sunwayworld.basemodule.business.operationbatch.bean.LimsPreTaskSupplementBean;
import com.sunwayworld.basemodule.business.operationbatch.service.LimsPreTaskSupplementService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;


@Repository
@GikamBean
public class LimsPreTaskSupplementServiceImpl implements LimsPreTaskSupplementService {

    @Autowired
    private LimsPreTaskSupplementDao limsPreTaskSupplementDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPreTaskSupplementDao getDao() {
        return limsPreTaskSupplementDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPreTaskSupplementBean limsPreTaskSupplement = jsonWrapper.parseUnique(LimsPreTaskSupplementBean.class);
        limsPreTaskSupplement.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsPreTaskSupplement);
        return limsPreTaskSupplement.getId();
    }

    @Override
    public Page<LimsPreTaskSupplementBean> selectPreTaskSupplementToColumns(Long operationBatch, RestJsonWrapperBean wrapper) {
        wrapper.removePageParams();
        MapperParameter parameter = wrapper.extractMapFilter();
        parameter.put("operationBatch", operationBatch);
        PageRowBounds rowBounds = wrapper.extractPageRowBounds();
        return selectPagination(() -> getDao().selectPreTaskSupplementToColumns(parameter), rowBounds);
    }

    @Override
    public void updateSupplementByPreFolder(Long preFolderId, RestJsonWrapperBean wrapper) {
        LimsPreTaskSupplementBean preTaskSupplementBean = wrapper.parseUnique(LimsPreTaskSupplementBean.class);
        preTaskSupplementBean.setPreFolderId(preFolderId);
        getDao().update(preTaskSupplementBean, ArrayUtils.asList("fieldValue"), "preFolderId", "fieldName");
    }
}
